﻿using StackExchange.Redis;

namespace RedisDemo.Redis;

/// <summary>
/// 缓存管理接口
/// </summary>
public interface ICacheManager
{
    /// <summary>
    /// 通过制定的Key获取缓存的数据，不存在则返回默认值
    /// </summary>
    /// <typeparam name="T">类型</typeparam>
    /// <param name="key">key</param>
    /// <returns>缓存的数据</returns>
    T Get<T>(string key);

    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">key</param>
    /// <param name="data">数据</param>
    /// <param name="cacheTime">缓存时间，分钟数</param>
    void Set(string key, object data, int cacheTime);

    public void SetAsync(string key, object data, int cacheTime);

    /// <summary>
    /// 添加缓存
    /// </summary>
    /// <param name="key">key</param>
    /// <param name="data">数据</param>
    /// <param name="cacheTime">缓存时间，分钟数</param>
    bool SetValue<T>(string key, T data, int cacheTime);

    public Task<bool> SetValueAsync<T>(string key, T data, int cacheTime);

    /// <summary>
    /// 验证缓存是否存在
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    bool IsSet(string key);
    Task<bool> IsSetAsync(string key);

    /// <summary>
    /// 移除缓存
    /// </summary>
    /// <param name="key"></param>
    void Remove(string key);

    /// <summary>
    /// 正则移除缓存
    /// </summary>
    /// <param name="pattern"></param>
    void RemoveByPattern(string pattern);

    /// <summary>
    /// 清理缓存
    /// </summary>
    void Clear();

    /// <summary>
    /// 自增
    /// <param name="key"></param> 
    /// <param name="value">自增的值</param> 
    /// </summary>
    long StringIncrement(string key, long value);

    /// <summary>
    /// 插入List
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="value"></param>
    public void ListSet<T>(string key, List<T> value);
    public Task ListSetAsync<T>(string key, List<T> value);

    /// <summary>
    /// 获取List
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <returns></returns>
    public List<T> ListGet<T>(string key);
    public Task <List<T>> ListGetAsync<T>(string key);

    /// <summary>
    /// 移除List对应的某一列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public long LisRemove<T>(string key, T value);
    public Task<long> LisRemoveAsync<T>(string key, T value);

    /// <summary>
    /// 加锁执行方法（无返回值）
    /// </summary>
    /// <param name="lockKey"></param>
    /// <param name="act"></param>
    /// <param name="expiry"></param>
    /// <param name="waitTime"></param>
    bool ExecuteWithLock(string lockKey, Action act, TimeSpan? expiry = null, TimeSpan? waitTime = null);

    /// <summary>
    /// 加锁执行方法（带返回值）
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="lockKey">锁key</param>
    /// <param name="func">获取锁成功后执行的委托</param>
    /// <param name="value">执行结果</param>
    /// <param name="expiry">锁到期时间，超时自动解锁</param>
    /// <param name="waitTime">最大等待时间（空值代表不等待）</param>
    /// <returns>执行结果</returns>
    bool ExecuteWithLock<T>(string lockKey, Func<T> func, out T value, TimeSpan? expiry = null, TimeSpan? waitTime = null);

    /// <summary>
    /// 添加一个值到Key
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <param name="score">排序分数，为空将获取集合中最大score加1</param>
    /// <returns></returns>
    Task<bool> SortedSetAddAsync<T>(string key, T value, double? score = null);

    /// <summary>
    /// 添加一个集合到Key
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <param name="score">排序分数，为空将获取集合中最大score加1</param>
    /// <returns></returns>
    Task<long> SortedSetAddAsync<T>(string key, List<T> value, double? score = null);


    /// <summary>
    /// 根据排序分数Score，删除从 start 开始的 stop 条数据
    /// </summary>
    /// <param name="key"></param>
    /// <param name="start"></param>
    /// <param name="stop"></param>
    /// <param name="exclude"></param>
    /// <returns></returns>
    Task<long> SortedSetRemoveRangeByScoreAsync(string key, double start, double stop, Exclude exclude = Exclude.None);


    /// <summary>
    /// 按照分数排序获取元素
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="start"></param>
    /// <param name="stop"></param>
    /// <param name="desc"></param>
    /// <returns></returns>
    Task<List<T>> SortedSetRangeByRankAsync<T>(string key, long start = 0, long stop = -1, bool desc = false);

    /// <summary>
    /// 删除元素
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    Task<bool> SortedSetRemoveAsync<T>(string key, T value);

    /// <summary>
    /// 通过分值删除元素
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="key"></param>
    /// <param name="start"></param>
    /// <param name="stop"></param>
    /// <returns></returns>
    Task<long> SortedSetRemoveRangeByScoreAsync<T>(string key, double start, double stop);
}
